<?php
/** .-------------------------------------------------------------------
 * |  Software: [HDCMS framework]
 * |      Site: www.hdcms.com
 * |-------------------------------------------------------------------
 * |    Author: 向军 <2300071698@qq.com>
 * |    WeChat: aihoudun
 * | Copyright (c) 2012-2019, www.houdunwang.com. All Rights Reserved.
 * '-------------------------------------------------------------------*/

namespace web\admin\controller;

class Goods extends Common{

	private $db;
	public function __construct()
	{
		parent::__construct();
		$this->db=new \system\model\Goods();
	}

	public function index(){
		$data=$this->db->get();
//        p($data);die;
		View::with('data',$data);
		View::make();
	}

	public function add(){

		if(IS_POST){
			//p($_POST);die;
			if($this->db->store()){
				message('添加成功','','success');
			}else{
				message($this->db->getError(),'back','error');
			}
		}
		$catedata=Data::tree(Db::table('category')->get(),'cname');
		//p($catedata);die;
		View::with('catedata',$catedata);
		$branddata=Db::table('brand')->get();
		//p($branddata);die;
		View::with('branddata',$branddata);
		View::make();
	}

	//异步删除图片
	public function removeImg(){
		unlink($_POST['path']);
	}

	//异步获取规格和属性
	//下拉框选中这个分类，分类里有tid指出他是什么类型，什么类型对应自身的什么属性
	public function ajaxGetAttr(){
		//p($_POST);die;
		$tid=$_POST['tid'];
		//根据tid获取类型属性表里面属性和规格
		$typeattrdata=Db::table('type_attr')->where('tid',$tid)->get();
		//p($typeattrdata);die;
		foreach($typeattrdata as $k=>$v){
			$typeattrdata[$k]['tavalue']=explode('|',$v['tavalue']);
		}
		//p($typeattrdata);die;
		echo json_encode($typeattrdata);die;
	}

	//修改
	public function edit(){
		$gid=q('get.gid',0,'intval');
		View::with('gid',$gid);

		if(IS_POST){
			//p($_POST);die;
			if($this->db->edit()){
				message('修改成功','','success');
			}else{
				message($this->db->getError(),'back','error');
			}
		}

		//获取旧数据
		//1.关联商品表和商品详情表，获取当前要编辑的单一数据
		$goodsdata=$this->db->join('detail','goods.gid','=','detail.gid')->where('goods.gid',$gid)->first();
		//p($goodsdata);die;
		View::with('goodsdata',$goodsdata);
		//获取多图
		$small=explode(',',$goodsdata['small']);
		foreach($small as $k=>$v){
			$small[$k]=dirname($v).'/'.ltrim(basename($v),'small_');
		}
		//p($small);die;
		View::with('small',$small);
		//2.获得分类数据
		$catedata=Data::tree(Db::table('category')->get(),'cname');
		//p($catedata);die;
		View::with('catedata',$catedata);
		//3.获得所有的品牌数据
		$branddata=Db::table('brand')->get();
		View::with('branddata',$branddata);
		//4.获得属性数据
		$attrdata=Db::table('type_attr')->where('class',1)->where('tid',$goodsdata['tid'])->get();
		foreach($attrdata as $k=>$v){
			$attrdata[$k]['tavalue']=explode('|',$v['tavalue']);
		}
		$goodsattr=Db::table('goods_attr')->where('gid',$gid)->lists('gavalue');
		View::with('attrdata',$attrdata);
		View::with('goodsattr',$goodsattr);
		//5.获得规格数据
		$goodspec=Db::table('type_attr')->join('goods_attr','type_attr.taid','=','goods_attr.taid')->where('class',2)->where('gid',$gid)->get();
		foreach($goodspec as $k=>$v){
			$goodspec[$k]['tavalue']=explode('|',$v['tavalue']);
		}
			View::with('goodspec',$goodspec);


		View::make();

	}
	//删除表
	public function del(){
		//p($_GET);die;
		$gid=q('get.gid',0,'intval');

		$goodsdata=$this->db->join('detail','goods.gid','=','detail.gid')->where('goods.gid',$gid)->first();

		View::with('goodsdata',$goodsdata);
		//获取多图,删除文件夹里面的图片

		$small=explode(',',$goodsdata['small']);
		$medium=explode(',',$goodsdata['medium']);
		$big=explode(',',$goodsdata['big']);
		foreach($small as $k=>$v){
			$small[$k]=dirname($v).'/'.ltrim(basename($v),'small_');
			unlink($v);
		}
		foreach($medium as $k=>$v){
			unlink($v);
		}
		foreach($big as $k=>$v){
			unlink($v);
		}
		foreach($small as $k=>$v){
			unlink($v);
		}
		if($goodsdata['pic']){
			unlink($goodsdata['pic']);
		}

		$this->db->where('gid',$gid)->delete();
		Db::table('detail')->where('gid',$gid)->delete();
		Db::table('goods_attr')->where('gid',$gid)->delete();
		message('删除成功','','success');
	}
}
